var editRowsNumber,_datasDatagrid,selectForm,createDate1,createDate2,createName,action;

function addRow() {
	if (editRowsNumber != -1) {
		_datasDatagrid.datagrid('endEdit', editRowsNumber);
	} else {
		_datasDatagrid.datagrid('beginEdit', 0);
		_datasDatagrid.datagrid('beginEdit', 0);
		editRowsNumber = 0;
	}
}

function deleteRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	if (rows.length > 0) {
		$.messager.confirm('请确认', '您确实要删除当前所有选择的记录吗？', function(b){
			if (b) {
				var ids = [];
				for ( var i = 0; i < rows.length; i++) {
					ids.push(rows[i].id);
				}
				$.post(sy.bp()+"/admin/operateLog/delete", {ids:ids.join(",")}, function(data) {
					if (data.result==1) {
						$.messager.show({msg:data.msg,title:'系统提示'});
						editRowsNumber = -1;
						_datasDatagrid.datagrid('unselectAll');
						_datasDatagrid.datagrid('reload');
					} else {
						$.messager.alert('警告', data.msg, 'warning');
					}
				}, "json");
			}
		});
	} else {
		$.messager.alert('提示', '请选择要删除的记录', 'error');
	}
}

function updateRow() {
	var rows = _datasDatagrid.datagrid('getSelections');
	//console.info(rows);
	switch (rows.length) {
	case 0:
		$.messager.alert('提示', '请选择要修改的记录', 'warning');
		break;
	case 1:
		if (editRowsNumber != -1) {
			_datasDatagrid.datagrid('endEdit', editRowsNumber);
		} else {
			var index = _datasDatagrid.datagrid('getRowIndex', rows[0]);
			_datasDatagrid.datagrid('beginEdit', index);
			editRowsNumber = index;
			_datasDatagrid.datagrid('unselectAll');
		}
		break;
	default:
		$.messager.alert('提示', '您选择了多条记录数，请每次修改一条记录', 'warning');
		break;
	}
}

function saveRow() {
	_datasDatagrid.datagrid('endEdit', editRowsNumber);
}

function rejectRow() {
	editRowsNumber = -1;
	_datasDatagrid.datagrid('rejectChanges');
	_datasDatagrid.datagrid('unselectAll');
}

function search_datas() {
	var formData = selectForm.form('getData', true);
	if(formData.createDate1 != '' && formData.createDate2 != '' && formData.createDate1 > formData.createDate2){
		$.messager.alert('提示', '开始时间不得大于结束时间，请重新选择！', 'warning');
	} else {
		if (selectForm.form('validate')) {
			_datasDatagrid.datagrid('load',sy.serializeObject(selectForm));
		}
	}
};

function reset_datas() {
	selectForm.find('input[type="text"]').val('');
	createDate1.datebox('setValue', '');
	createDate2.datebox('setValue', '');
	_datasDatagrid.datagrid('load',{});
	createName.val('');
	action.val('');
}

function search_exception() {
	$.post(sy.bp()+"/admin/operateLog/list", {"page":1,"rows":50,"sort":"createTime","order":"desc","isException":"1"}, function(data) {
		if (data) {
			_datasDatagrid.datagrid('loadData', data);
		}
	}, "json");
}

$(function(){
	editRowsNumber = -1;
	_datasDatagrid = $('#_datasDatagrid');
	selectForm = $('#selectForm');
	createDate1 = $('#createDate1').datebox({editable:false}).datebox('activeTextArrow');
	createDate2 = $('#createDate2').datebox({editable:false}).datebox('activeTextArrow');
	createName = $('#createName').validatebox({validType:['unnormal','maxLength[200]']});
	action = $('#action').validatebox({validType:['unnormal','maxLength[200]']});
	
	_datasDatagrid.datagrid({
		url : sy.bp()+'/admin/operateLog/list',
		fit : true,
		autoRowHeight : false,
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : false,
		fitColumns : true,
		pageSize : 50,
		pageList : [50,100,150,200],
		nowrap : true,
		border : false,
		idField : 'id',
		sortName : 'createTime',
		sortOrder : 'desc',
		view: sy.myview,
        emptyMsg: '没有查询到相关数据',
		toolbar: [
					{text:'批量删除',iconCls: 'icon-remove',handler: deleteRow},'-',
					{text:'撤销',iconCls: 'icon-redo',handler: rejectRow},'-',
					{text:'刷新',iconCls: 'icon-reload',handler: function(){_datasDatagrid.datagrid('reload');}}
				 ],
		frozenColumns: [[
		                 	{field:'id',title:'编号',align:'center',width:100,sortable:true,checkbox:true},
		     				{field:'createTime',title:'时间',align:'center',width:125,sortable:true},
		     				{field:'createName',title:'操作员',align:'center',width:75,sortable:true},
		     				{field:'ip',title:'IP',align:'center',width:100,sortable:true}
					   ]],
		columns:[[
		          {field:'action',title:'内容',align:'left',width:300},
		          {field:'operate',title:'操作',align:'left',width:100},
		          {field:'exception',title:'异常信息',align:'left',width:300}
				]],
		onLoadSuccess : function(data) {
			editRowsNumber = -1;
			$.messager.progress('close');
		},
//		onDblClickRow : function(rowIndex, rowData) {
//			if (editRowsNumber != -1) {
//				_datasDatagrid.datagrid('endEdit', editRowsNumber);
//			} else {
//				_datasDatagrid.datagrid('beginEdit', rowIndex);
//				editRowsNumber = rowIndex;
//				_datasDatagrid.datagrid('unselectAll');
//			}
//		},
		onRowContextMenu : function(e, rowIndex, rowData){
			e.preventDefault();	/*阻止浏览器右键菜单*/
			_datasDatagrid.datagrid('unselectAll').datagrid('selectRow', rowIndex);
			$('#datagridContextMenu').menu('show', {left:e.pageX,top:e.pageY /*取当前鼠标X和Y坐标*/});
		},
		onAfterEdit : function(rowIndex, rowData, changes) { /*结束编辑时调用的事件*/
			//console.info(rowData); 	/*rowData表示最后编辑的那些行数据*/
			//console.info(changes); 	/*changes表示更改的数据列，是一个数组*/
			var inserted = _datasDatagrid.datagrid('getChanges', 'inserted');
			//console.info(inserted);	/*inserted表示增加的那些行数据*/
			var updated = _datasDatagrid.datagrid('getChanges', 'updated');
			//console.info(updated);	/*updated表示编辑的那些行数据*/
			if (inserted.length < 1 && updated.length < 1) {
				editRowsNumber = -1;
				_datasDatagrid.datagrid('unselectAll');
				return;
			}
			var url='';
			if (inserted.length > 0) {
				url=sy.bp()+'/admin/operateLog/add';
			}
			if (updated.length > 0) {
				url=sy.bp()+'/admin/operateLog/update';
			}
			$.post(url, rowData, function(data) {
				if (data && data.result==1) {
					_datasDatagrid.datagrid('acceptChanges');
					$.messager.show({msg:data.msg,title:'系统提示'});
					_datasDatagrid.datagrid('reload');
				} else {
					_datasDatagrid.datagrid('rejectChanges');
					$.messager.alert('错误', data.msg, 'warning');
				}
				editRowsNumber = -1;
				_datasDatagrid.datagrid('unselectAll');
			}, "json");
		}
	});
	
	$(document).keydown(function(event){
		if(event.keyCode==13){
			search_datas();
		} 
	});
});